package com.ljd.repository.impl;

import com.ljd.entity.Admin;
import com.ljd.repository.AdminRepository;
import com.ljd.utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminRepositoryImpl implements AdminRepository {
    @Override
    public Admin login(String username, String password) {
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs = null;
        Admin admin=null;
        String sql = "select * from bookadmin where username=? and password=?";
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if(rs.next()) {
                int id=rs.getInt(1);
                admin = new Admin(id, username, password);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCTools.release(connection,ps,rs);
        }
        return admin;
    }
}
